package pro.view.core;

import java.awt.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.JLayeredPane;

/**
 * @author  sangwo.y
 */
public class Cell implements IHolder {
	
	private int id;
	private GameView gv;
	private Card card;
	private int offsetX;
	private int offsetY;
	
	public Cell(int id, GameView gv, Point p) {
		this.id=id;
		this.gv=gv;
		offsetX = p.x;
		offsetY = p.y;		
		card = null;
		//tmp
//		JLabel jl = new JLabel();
//		jl.setBounds(p.x, p.y, GameView.CARD_WIDTH, GameView.CARD_HEIGHT);
//		jl.setBorder(BorderFactory.createLineBorder(Color.black));
//		gv.getContentPane().add(jl, new Integer(-1));	
	}

	public boolean add(Card card) {
		//System.out.println(" in ");
		if (this.card!=null) {
			gv.getContentPane().remove(this.card);
			gv.repaint();
//			((JLayeredPane)gv.getContentPane()).setLayer(this.card, new Integer(-1));
			//System.out.println("whatafuck");
		}
		gv.getContentPane().add(card, new Integer(1));
		card.setHolder(this);
		this.card=card;
//		System.out.println(offsetX+" "+offsetY);
		//card.setLocation(offsetX, offsetY);//, GameView.CARD_WIDTH, GameView.CARD_HEIGHT);
//		System.out.println(" after ");
		return true;
	}

	public List<Card> cardsToDrag(Card card) {
		return getCards();
	}

	public boolean contains(Point p) {

		
		if (	(p.x>=offsetX) &&  (p.y>=offsetY) &&
				(p.x<=offsetX+GameView.CARD_WIDTH) && 
				(p.y<=offsetY+GameView.CARD_HEIGHT+GameView.ACCEPT_SUPL_Y_SPACE)	) {			
			return true;
		}
		return false;
	}

	public List<Card> getCards() {
		List<Card> cards = new ArrayList<Card>();
		if (card!=null) cards.add(card);
		return cards;
	}

	/**
	 * @return  the id
	 * @uml.property  name="id"
	 */
	public int getId() {
		return id;
	}

	public List<Card> removeCards(int n) {
		List<Card> oldCard = new ArrayList<Card>();
		if (n==1) {			
			oldCard = getCards();
			card=null;
		}
		return oldCard;
	}

	public void renderContent() {
		if (card!=null) {
			((JLayeredPane)gv.getContentPane()).setLayer(card, new Integer(1));
			
			
			Point p = card.getLocation();
			boolean keepMoving = true;
			int dirX, dirY;
			if (p.x>offsetX) {dirX=-1; } else {dirX=1;}
			if (p.y>offsetY) {dirY=-1; } else {dirY=1;}
			while (keepMoving) {
				if (p.x!=offsetX) p.x+=dirX; 
				if (p.y!=offsetY) p.y+=dirY;
				card.setLocation(p.x,p.y);
				if (	(p.x==offsetX)	&&	(p.y==offsetY)	)
					keepMoving=false;
			}		
			
		}
	}

	public int size() {
		return (card==null)?0:1 ;
	}
	
	public void delete() {
		if (card!=null) {
			gv.getContentPane().remove(card);
		}
	}

}
